//
//  StatusCell.swift
//  WeChat
//
//  Created by 梁迪 on 15/12/20.
//  Copyright © 2015年 梁迪. All rights reserved.
//

import UIKit
import SnapKit


class StatusCell: UITableViewCell {
    
    //
    var bottomViewTopConstrains:Constraint?
    
    
    
    //定义微博模型
    var status:Status?{
        
        didSet{
            
            
            topView.status = status
//            retweetedView.retweetedStatus = status?.retweeted_status
            
            bottomViewTopConstrains?.uninstall()
            if let retweetedStatus = status?.retweeted_status {
                
                //一定是转发微博
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomViewTopConstrains = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
                
                //设置转发微博的数据源
                retweetedView.retweetedStatus = retweetedStatus
                
                //显示转发微博
                retweetedView.hidden = false
                
                
            }else {
                
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomViewTopConstrains = make.top.equalTo(topView.snp_bottom).constraint
                })
                
                retweetedView.hidden = true
                
        
                
            }
            
            
            
            
            
            
            
            
            
            
            
        }
        
    }
    
    //cell入口
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        selectionStyle = .None
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        //使用 contentView 添加子视图
        
        contentView.addSubview(topView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweetedView)
        
        
        //设置 frame
        
        
        
        
        
        
        topView.snp_makeConstraints { (make) -> Void in
            
            make.top.left.right.equalTo(contentView)
            
    
        }
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(contentView)
            make.top.equalTo(topView.snp_bottom)
//            make.height.equalTo(50)
            
        }

        bottomView.snp_makeConstraints { (make) -> Void in
            
            make.right.left.equalTo(self)
           self.bottomViewTopConstrains =  make.top.equalTo(retweetedView.snp_bottom).constraint
            make.height.equalTo(40)
        }
        
       
        
        
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.top.right.left.equalTo(self)
            make.bottom.equalTo(bottomView.snp_bottom)
        }
        

        
        
    }
    
    private lazy var topView:StatusCellTopView = StatusCellTopView()
    private lazy var bottomView:StatusCellBottomView = StatusCellBottomView()
    private lazy var retweetedView:StatusRetweetedView = StatusRetweetedView()
}
